//
//  CLShareManager+ShareView.m
//  CLShare
//
//  Created by ClaudeLi on 16/5/4.
//  Copyright © 2016年 ClaudeLi. All rights reserved.
//

#import "CLShareManager+ShareView.h"
#import "CLShareButton.h"

@implementation CLShareManager (ShareView)

- (void)creatShareView{
    self.shareBgView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self.shareBgView];
    [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:self.shareBgView];
    
    UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 200)];
    topView.userInteractionEnabled = YES;
    [self.shareBgView addSubview:topView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenShareView)];
    [topView addGestureRecognizer:tap];
    
    self.shareView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 346*PERCEN)];
    self.shareView.backgroundColor = [UIColor whiteColor];
    [self.shareBgView addSubview:self.shareView];
    
    UILabel * shareLeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20*PERCEN, ScreenWidth, 15)];
    shareLeLabel.text = NSLocalizedString(@"分享到", nil);
    shareLeLabel.textColor = [UIColor grayColor];
    shareLeLabel.textAlignment = NSTextAlignmentCenter;
    [self.shareView addSubview:shareLeLabel];
    UIView *line1View = [[UIView alloc] initWithFrame:CGRectMake(50*PERCEN, 32*PERCEN, 265*PERCEN, 1)];
    line1View.backgroundColor = UIColorFromHex(0xcccccc);
    [self.shareView addSubview:line1View];
    UIView *line2View = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-265*PERCEN-50*PERCEN, 32*PERCEN, 265*PERCEN, 1)];
    line2View.backgroundColor = UIColorFromHex(0xcccccc);
    [self.shareView addSubview:line2View];

    CGFloat itemWidth = 110*PERCEN;
    UIScrollView * shareScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(shareLeLabel.frame)+32*PERCEN, ScreenWidth, 234*PERCEN)];
    shareScrollView.contentSize = CGSizeMake(itemWidth*shareNameArray.count, 0);
    shareScrollView.showsVerticalScrollIndicator = NO;
    shareScrollView.bounces = NO;
    shareScrollView.showsHorizontalScrollIndicator = NO;
    shareScrollView.backgroundColor = [UIColor whiteColor];
    [self.shareView addSubview:shareScrollView];
    
    for (int i = 0; i < shareNameArray.count; i++) {
        
        CLShareButton * shareBtn = [[CLShareButton alloc] initWithFrame:CGRectMake(((itemWidth +24*PERCEN)* i)+50*PERCEN, 0,110*PERCEN,shareScrollView.height-32*PERCEN)];
        if (ISIPAD) {
            itemWidth = 120*PERCEN;
            shareLeLabel.frame = CGRectMake(0, 50*PERCEN, ScreenWidth, 15);
            shareLeLabel.font = [UIFont systemFontOfSize:15.f];
            shareLeLabel.textColor = UIColorFromHex(0x333333);
            line1View.frame =CGRectMake(440*PERCEN, 60*PERCEN, 520*PERCEN, 1);
            line2View.frame = CGRectMake(SCREEN_WIDTH-440*PERCEN-520*PERCEN, 60*PERCEN, 520*PERCEN, 1);
            shareBtn.frame = CGRectMake(((itemWidth +80*PERCEN)* i)+560*PERCEN, 0,120*PERCEN,shareScrollView.height-40*PERCEN);
        }
        [shareBtn setImage:[UIImage imageNamed:shareImageArray[i]] forState:UIControlStateNormal];
        [shareBtn setTitle:shareNameArray[i] forState:UIControlStateNormal];
        shareBtn.tag = i;
        
        [shareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        [shareScrollView addSubview:shareBtn];
    }
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelBtn.frame = CGRectMake(0, shareScrollView.bottom, self.shareView.frame.size.width, 80*PERCEN);
    
    cancelBtn.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    cancelBtn.layer.borderWidth = 0.8f;
    cancelBtn.tintColor = [UIColor grayColor];
    [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(hiddenShareView) forControlEvents:UIControlEventTouchUpInside];
    if (ISIPAD) {
        cancelBtn.tintColor = UIColorFromHex(0x333333);
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16.f];
    }
    [self.shareView addSubview:cancelBtn];
    self.shareBgView.hidden = YES;
}

@end
